﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

using Jungle.Code.Utils;
using Jungle.Code.DAL;
using Jungle.Code.BLL;
using Jungle.Code.Core;
using Jungle.Prodamo.Core;
using Jungle.Prodamo.PriceManager;

public partial class UserInfo : BaseUserModule 
{
    private int userId;
    SessionKeeper thisSession = new SessionKeeper();
    protected override void OnInit(EventArgs e)
    {
        thisSession.SessionId = Session.SessionID.ToString();
        thisSession.GetData();
        userId = thisSession.UserId;

        if (thisSession.UserId == -1) div_1.Visible = false;

        btn_UpdateUser.PostBackUrl = Request.RawUrl;
        btn_AddEstim.PostBackUrl = Request.RawUrl;

        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["uId"] != null)
        {
            if (!IsPostBack)
            {
                UserInfoMenu.ActiveViewIndex = 0;
                LoadData(Convert.ToInt32(Request["uId"]));
            }
        }
        else
        {
            if (thisSession.UserId != -1)
            {
                UserInfoMenu.ActiveViewIndex = 1;
                userId = thisSession.UserId;
                if (!IsPostBack)
                {
                    LoadData(userId);
                }
            }
        }
    }
    private void LoadData(int id_user)
    {
        for (int i = 1; i < 31; i++)
        {
            ddl_day.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        for (int i = 1900; i < 2008; i++)
        {
            ddl_year.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        UserManager um = new UserManager();

        um.UserId = id_user;
        um.GetById();

        tb_fname.Text = um.Fname;
        tb_iname.Text = um.Iname;
        tb_oname.Text = um.Oname;
        tb_organization.Text = um.Organization;
        tb_phone.Text = um.Phone;
        tb_faks.Text = um.Faks;
        tb_email.Text = um.Email;
        tb_erdpou.Text = um.Edrpou;
        tb_zip.Text = um.Zip;
        tb_address.Text = um.Address;
        ddl_country.SelectedValue = um.Country;
        ddl_day.SelectedValue = um.BirthDay.Day.ToString();
        if (!um.Sex) ddl_gender.SelectedValue = "0";
        else ddl_gender.SelectedValue = "1";
        ddl_month.SelectedValue = um.BirthDay.Month.ToString();
        ddl_year.SelectedValue = um.BirthDay.Year.ToString();
        ddl_city.SelectedValue = um.City;

        img_Logo.ImageUrl = um.Logo;
        img_Map.ImageUrl = um.Map;
        tb_Web.Text = um.Web;
        tb_WorkTime.Text = um.WorkTime;
        tb_Deliver.Text = um.Deliver;
        tb_Credit.Text = um.Credit;

        if (um.UsertType)
        {
            ur1.Visible = false;
            ur2.Visible = false;
            ur3.Visible = false;
            ur4.Visible = false;
            ur5.Visible = false;
            ur6.Visible = false;
            ur8.Visible = false;
            ur9.Visible = false;
            ur10.Visible = false;
            ur11.Visible = false;
            ur12.Visible = false;
            ur13.Visible = false;
        }
        else
        {
            ur7.Visible = false;
            fz1.Visible = false;
        }

        lb_address.Text = ddl_country.SelectedItem.Text + ", " + ddl_city.SelectedItem.Text + ", " + um.Address;
        lb_email.Text = um.Email;
        lb_erdpou.Text = um.Edrpou;
        lb_faks.Text = um.Faks;
        lb_fio.Text = um.Fname + " " + um.Iname + " " + um.Oname;
        lb_gender.Text = ddl_gender.SelectedItem.Text;
        lb_nickname.Text = um.NickName;
        lb_organization.Text = um.Organization;
        lb_phone.Text = um.Phone;

        if (!um.UsertType)
        {
            if(UserInfoMenu.ActiveViewIndex == 0) UserInfoMenu.ActiveViewIndex = 2;

            Price p = new Price();
            p.UserId = um.UserId;
            img_c_Logo.ImageUrl = um.Logo;
            lb_c_Address.Text = um.Address;
            lb_c_City.Text = ddl_city.SelectedItem.Text;
            lb_c_CountGoods.Text = p.PriceCount().ToString();
            lb_c_Credit.Text = um.Credit;
            lb_c_Deliver.Text = um.Deliver;
            lb_c_Email.Text = um.Email;
            lb_c_Name.Text = um.Organization;
            lb_c_Phone.Text = um.Phone;
            hp_Web.NavigateUrl = um.Web;
            hp_Web.Text = um.Web;
            lb_c_WorkTime.Text = um.WorkTime;
            img_c_Map.ImageUrl = um.Map;

            Estims1.EstimType = 1;
            Estims1.ItemId = um.UserId;
            Estims1.Visible = true;

            PriceList1.Visible = true;
            PriceList1.GetBy = 5;
            PriceList1.iId = um.UserId;

            btn_OpenAddForm.Visible = true;

               
        }

    }
    protected void UpdateUser(object sender, EventArgs e)
    {
        UserManager um = new UserManager();
        um.UserId = userId;
        um.GetById();

        ValidBlock.InnerHtml = "";
        bool valid = true;
        if (tb_address.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите адрес<br/>"; }
        if (tb_email.Text.Trim().Length < 1 || tb_email.Text.IndexOf("@") == -1) { valid = false; ValidBlock.InnerHtml += "Укажите email<br/>"; }
        if (tb_erdpou.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите код ЕДРПОУ<br/>"; }
        if (tb_fname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите фамилию<br/>"; }
        if (tb_iname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите имя<br/>"; }
        if (tb_oname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите отчество<br/>"; }
        if (tb_organization.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите название оранизации<br/>"; }
        if (tb_zip.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите почотвый код<br/>"; }

        if (valid)
        {
            um.Fname = tb_fname.Text;
            um.Iname = tb_iname.Text;
            um.Oname = tb_oname.Text;
            um.Organization = tb_organization.Text;
            um.Phone = tb_phone.Text;
            um.Faks = tb_faks.Text;
            um.Email = tb_email.Text;
            um.Edrpou = tb_erdpou.Text;
            um.Zip = tb_zip.Text;
            um.Address = tb_address.Text;
            um.Country = ddl_country.SelectedValue;
            um.City = ddl_city.SelectedValue;
            um.BirthDay = Convert.ToDateTime(ddl_day.SelectedValue + "." + ddl_month.SelectedValue + "." + ddl_year.SelectedValue);
            um.Web = tb_Web.Text.Trim();
            um.WorkTime = tb_WorkTime.Text.Trim();
            um.Deliver = tb_Deliver.Text.Trim();
            um.Credit = tb_Credit.Text.Trim();
            if (fu_Logo.HasFile)
            {
                fu_Logo.PostedFile.SaveAs(Server.MapPath("Images/Firms/" + Session.SessionID + "_" + fu_Logo.FileName));
                um.Logo = "~/Images/Firms/" + Session.SessionID + "_" + fu_Logo.FileName;                
            }
            if (fu_Map.HasFile)
            {
                fu_Map.PostedFile.SaveAs(Server.MapPath("Images/Firms/" + Session.SessionID + "_" + fu_Map.FileName));
                um.Map = "~/Images/Firms/" + Session.SessionID + "_" + fu_Map.FileName;              
            }

            um.Update();
        }



    }
    protected void AddEstim(object sender, EventArgs e)
    {
        Estimation es = new Estimation();
        es.EstimType = 1;
        es.ItemId = Convert.ToInt32(Request["uId"]);
        es.Raiting = Convert.ToInt32(ddl_Raiting.SelectedValue);
        es.Advant = tb_Advant.Text.Trim();
        es.Faling = tb_Faling.Text.Trim();
        es.Description = tb_Description.Text.Trim();
        es.TimeUse = "0 дней";
        es.BuyPlace = "у вас";
        es.UserId = thisSession.UserId;
        if (ddl_Advice.SelectedValue == "1") es.Advice = true;
        else es.Advice = false;
        es.Add();

        Response.Redirect(Request.RawUrl);

    }
}
